package com.android.questions.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.config.LoginConfig;
import com.android.questions.databinding.DialogGoWithdrawalBinding;
import com.android.questions.util.AnimUtil;
import com.dq.ad.AdManager;

/**
 * 去提现
 * <p>
 * Author: admin
 * Date: 2021/11/16 16:05
 * Description:
 */
public class DialogGoWithdrawal extends Dialog implements View.OnClickListener {

    private DialogGoWithdrawalBinding goWithdrawalBinding;
    private OnClickLister clickLister;
    private final Activity activity;
    private int type = 0; // 0 新人提现 1 每日提现
    private double coin = 1000; // 红包券

    public DialogGoWithdrawal(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        goWithdrawalBinding = DialogGoWithdrawalBinding.inflate(activity.getLayoutInflater());
        setContentView(goWithdrawalBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        ActivityClickUtil.getInstance().clear();
        AnimUtil.receiveAnim(activity, goWithdrawalBinding.imgReceive);
        int cash = (int) LoginConfig.getUserCash();
        String title = "当前拥有红包券";
        if (1 == type) {
            cash = (int) coin;
            title = "成功提取出一笔红包券";
        }
        goWithdrawalBinding.tvTitle.setText(title);
        goWithdrawalBinding.tvCoin.setText(cash + "");

        AdManager.getInstance().showNativeAD(activity, goWithdrawalBinding.nativeContainer, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                goWithdrawalBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                goWithdrawalBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {
            }

            @Override
            public void onClick() {
            }

            @Override
            public void onShow(View view) {

            }
        });
    }

    private void initLister() {
        goWithdrawalBinding.imgReceive.setOnClickListener(this);
        goWithdrawalBinding.ivClose.setOnClickListener(this);
    }

    public DialogGoWithdrawal setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    public DialogGoWithdrawal setType(int type) {
        this.type = type;
        return this;
    }

    public DialogGoWithdrawal setCoin(double coin) {
        this.coin = coin;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (v.getId() == R.id.imgReceive) {
            if (this.clickLister != null) {
                this.clickLister.onWithdrawal();
            }
        }
    }

    public interface OnClickLister {
        void onWithdrawal();
    }
}